<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/28
 * Time: 11:21
 */

namespace Admin\Controller;
use Admin\Controller\BaseController;
use Common\ORG\Util\UploadFile;

class OrderBillController extends BaseController
{

    /**
     * 构造函数
     * ItemController constructor.
     */
    public function __construct()
    {
        //无需进行权限检测的功能
        \Org\Auth\AuthUtil::setNoAuthFuncList([
            '/admin/orderBill/uploadOrderBill',
            '/admin/orderBill/doUploadOrderBill',
        ]);
        parent::__construct();
    }

    /**
     * 发票合同列表
     */
    public function orderBillList(){
        //分页
        $page = I('get.page', 1);
        $pagesize = 15;
        $company_name = I('get.company_name');
        $type = I('get.type',0);
        $flag = I('get.flag',0);
        $admin_name = I('get.admin_name');
        $department = I('get.department');

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $admin_name && $params['admin_name'] = $admin_name;
        $department && $params['department_name'] = $department;
        $company_name && $params['company_name'] = $company_name;
        $type && $params['type'] = $type;
        switch ($flag){
            case 1:
                $params['status'] = ['lt',15];
                break;
            case 2:
                $params['status'] = 15;
                break;
            default:
                break;
        }
        $result = D('OrderBill','Logic')->searchOrderBillList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $billList = $result['dataList'];
        //生成分页
        $url = "/orderBill/orderBillList?flag={$flag}&company_name={$company_name}&type={$type}&department={$department}&admin_name={$admin_name}";
        $page_html = create_page($url, $total, $page, $page_size);
        $billStatusList = C('BILL_STATUS');

        //发票类型
        $bill_typeList = C('BILL_TYPE');

        $this->assign('bill_typeList',$bill_typeList);
        $this->assign('page_html',$page_html);
        $this->assign('page',$page);
        $this->assign('page_size',$page_size);
        $this->assign('total_page',$total_page);
        $this->assign('total',$total);
        $this->assign('billList',$billList);
        $this->assign('billStatusList',$billStatusList);

        $this->assign('company_name', $company_name);
        $this->assign('type', $type);
        $this->assign('flag', $flag);
        $this->assign('department', $department);
        $this->assign('admin_name', $admin_name);
        $this->loadFrame('orderBillList');
    }

    /**
     * 上传发票文件
     */
    public function uploadOrderBill(){
        $bill_id = intval(I('get.id'));
        $result = D('Resource/OrderBill','Service')->getOrderBillInfo(['id'=>$bill_id]);
        $bill_typeList = C('BILL_TYPE');

        $this->assign('billInfo',$result);
        $this->assign('bill_typeList',$bill_typeList);
        $this->loadFrame('uploadOrderBill');
    }

    /**
     * 上传保存
     */
    public function doUploadOrderBill(){
        $user = self::$user;
        $order_id = isset($_POST['order_id'])?intval($_POST['order_id']):'';
        $bill_id = isset($_POST['bill_id'])?intval($_POST['bill_id']):'';
        $admin_id = isset($_POST['admin_id'])?intval($_POST['admin_id']):'';
        $type = isset($_POST['type'])?intval($_POST['type']):'';
        $order_rate = isset($_POST['order_rate'])?strval($_POST['order_rate']):'';
        $company_title = isset($_POST['company_title'])?strval($_POST['company_title']):'';
        $bill_title = isset($_POST['bill_title'])?strval($_POST['bill_title']):'';
        $protocol_number = isset($_POST['protocol_number'])?strval($_POST['protocol_number']):'';
        $item_type = isset($_POST['item_type'])?strval($_POST['item_type']):'';
        $is_filing = isset($_POST['is_filing'])?intval($_POST['is_filing']):'';
        $item_name = isset($_POST['item_name'])?strval($_POST['item_name']):'';
        $arrival_time = isset($_POST['arrival_time'])?$_POST['arrival_time']:'';
        $amount = isset($_POST['amount'])?floatval($_POST['amount']):0.00;
        $description = isset($_POST['description'])?strval($_POST['description']):'';
        $send_type = isset($_POST['send_type'])?intval($_POST['send_type']):'';
        $taxpayer_id_number = isset($_POST['taxpayer_id_number'])?strval($_POST['taxpayer_id_number']):'';
        $address = isset($_POST['address'])?strval($_POST['address']):'';
        $send_address = isset($_POST['send_address'])?strval($_POST['send_address']):'';
        $bank = isset($_POST['bank'])?strval($_POST['bank']):'';
        $bank_account = isset($_POST['bank_account'])?strval($_POST['bank_account']):'';
        $mobile = isset($_POST['mobile'])?strval($_POST['mobile']):'';
        $bill = isset($_POST['bill'])?strval($_POST['bill']):'';
        $contract = isset($_POST['contract'])?strval($_POST['contract']):'';
        $contract_qualification = isset($_POST['contract_qualification'])?strval($_POST['contract_qualification']):'';
        $materiel = isset($_POST['materiel'])?strval($_POST['materiel']):'';
        $bill_no = isset($_POST['bill_no'])?strval($_POST['bill_no']):'';

        $receipts_time = isset($_POST['receipts_time'])?strtotime($_POST['receipts_time']):'';
        $receipts_type = isset($_POST['receipts_type'])?strval($_POST['receipts_type']):'';
        $receipts_no = isset($_POST['receipts_no'])?strval($_POST['receipts_no']):'';
        $receipts_desc = isset($_POST['receipts_desc'])?strval($_POST['receipts_desc']):'';
        $receipts_remark = isset($_POST['receipts_remark'])?strval($_POST['receipts_remark']):'';

        if(!$order_rate)
            return Response(2001,"请填写订单占比");
        if(!$company_title)
            return Response(2001,"请填写开票公司抬头");
        if(!$bill_title)
            return Response(2001,"请填写开票抬头");
        if(!$protocol_number)
            return Response(2001,"请填写协议号");
        if(!$item_name)
            return Response(2001,"请填写客户产品名称");
        if(!$item_type)
            return Response(2001,"请填写产品类型");
        if(!$arrival_time)
            return Response(2001,"请填写预计到账时间");
        if(!$description)
            return Response(2001,"请填写开票内容");
        if(!$amount)
            return Response(2001,"请填写开票金额");
        if(!$send_type)
            return Response(2001,"请填写寄送方式");

        if(($type==2) && !$taxpayer_id_number)return Response(2002,"请填写纳税人识别号");
        if(($type==2) && !$address)return Response(2002,"请填写地址");
        if(($type==2) && !$bank)return Response(2002,"请填写开户行");
        if(($type==2) && !$bank_account)return Response(2002,"请填写开户行账号");
        if(($type==2) && !$mobile)return Response(2002,"请填写联系电话");

        $data = [];
        $data['order_rate'] = $order_rate;
        $data['order_id'] = $order_id;
        $data['admin_id'] = $admin_id;
        $data['bill_id'] =  $bill_id;
        $data['admin_id'] = $admin_id;
        $data['company_title']=$company_title;
        $data['bill_title']=$bill_title;
        $data['protocol_number']=$protocol_number;
        $data['is_filing']=$is_filing;
        $data['type']=$type;
        $data['item_name']=$item_name;
        $data['item_type']=$item_type;
        $data['arrival_time']=$arrival_time;
        $data['amount']=$amount;
        $data['description']=$description;
        $data['send_type']=$send_type;
        $data['status']=15;
        $taxpayer_id_number && $data['taxpayer_id_number']=$taxpayer_id_number;
        $address && $data['address']=$address;
        $send_address && $data['send_address']=$send_address;
        $bank && $data['bank']=$bank;
        $bank_account && $data['bank_account']=$bank_account;
        $mobile && $data['mobile']=$mobile;
        $bill_no && $data['bill_no']=$bill_no;
        $materiel && $data['materiel']=$materiel;
        $bill && $data['bill']=$bill;
        $contract && $data['contract']=$contract;
        $contract_qualification && $data['contract_qualification']=$contract_qualification;

        $receipts_time && $data['receipts_time']=$receipts_time;
        $receipts_type && $data['receipts_type']=$receipts_type;
        $receipts_no && $data['receipts_no']=$receipts_no;
        $receipts_desc && $data['receipts_desc']=$receipts_desc;
        $receipts_remark && $data['receipts_remark']=$receipts_remark;

        D('OrderBill','Logic')->ajaxSaveApplyBill($data);
    }


}